<?php

use yii\helpers\Html;
use yii\grid\GridView;

use app\models\MGoods;
use app\models\MGoodsSearch;

use app\models\MUser;

use kartik\editable\Editable;

/* @var $this yii\web\View */
/* @var $searchModel app\models\MGoodsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

use app\models\U;


$this->title = '商品管理';
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="mgoods-index">

    <h4><?= Html::encode($this->title) ?></h4>

    <p>
        <?= Html::a('新增商品', ['create'], ['class' => 'btn btn-success']) ?>
    </p>


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'showOnEmpty'=>false,
        'layout'=>"{items}{pager}\n{summary}",

        'tableOptions' =>['class' => 'table table-striped'],
        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],
            /*
            [
                'attribute' => 'list_img_url',
                'label' => '',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                    if(!empty($model->list_img_url))
                        $list_img_url = '<img src=' . $model->list_img_url .' width=48px height=48px>';
                    else
                        $list_img_url = '';

                    return $list_img_url;
                },
                'filter'=> false,
                'headerOptions' => array('style'=>'width:80px;'),
            ],
            */
            //'goods_id',
            [
                'attribute' => 'goods_id',
                'label' => '编号',
                'headerOptions' => array('style'=>'width:60px;'),
            ],

            //'title',
            [
                'attribute' => 'title',
                'label' => '商品名',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                    if(!empty($model->list_img_url))
                        $list_img_url = '<img src=' . $model->list_img_url .' width=100px height=70px>';
                    else
                        $list_img_url = '';

                    return $list_img_url."<br>".$model->title;
                },
                'headerOptions' => array('style'=>'width:180px;'),
            ],

            //'parameter',

            [
                'attribute' => 'parameter',
                'label' => '参数',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                    return  mb_substr($model->parameter, 0, 20, 'utf-8')."...";
                },
                'headerOptions' => array('style'=>'width:180px;'),
            ],

            [
                'attribute' => 'attachment',
                'label' => '附件',
                'headerOptions' => array('style'=>'width:120px;'),
            ],

            //'goods_kind',
            [
                'attribute' => 'goods_kind',
                'label' => '分类',
                'value'=>function ($model, $key, $index, $column) { 
                        return MGoods::getGoodsKindOption($model->goods_kind); 
                 },
                'filter'=> MGoods::getGoodsKindOption(),
                'headerOptions' => array('style'=>'width:120px;'),
            ],

            [
                'attribute' => 'brand',
                'label' => '品牌',
                'value'=>function ($model, $key, $index, $column) {
                    return MGoods::getBrandOption($model->brand);
                },
                'filter'=> MGoods::getBrandOption(),
                'headerOptions' => array('style'=>'width:120px;'),
            ],
            //'descript',
            //'price',
            //[
            //    'attribute' => 'price',
            //    'headerOptions' => array('style'=>'width:80px;'),
            //],

            //'price_hint',
            //'price_old',
            //[
             //   'attribute' => 'price_old',
              //  'headerOptions' => array('style'=>'width:80px;'),
            //],            
            // 'price_old_hint',
            // 'detail:ntext',
            // 'list_img_url:url',

            // 'body_img_url:url',
            /*
            [
                'attribute' => 'body_img_url',
                'label' => '大图',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                        $len = 0;
                        $imgHtml = "";
                        $imgs = explode(";",$model->body_img_url);
                        foreach ($imgs as $img) {
                            $len++;
                            if(sizeof($imgs) == $len) break; //分号分割后，数组最后一项为空，剔除
                            $imgHtml = $imgHtml . '<img src=' . $img . ' width=45px height=45px>';
                        }
                        return $imgHtml;
                },    
                'headerOptions' => array('style'=>'width:160px;'),      
            ],
            */


            // 'quantity',
            // 'office_ctrl',
            // 'package_ctrl',
            //'detail_ctrl',
			/*
            [
                'attribute' => 'detail_ctrl',
                'label' => '显示详情',
                'value'=>function ($model, $key, $index, $column) { 
                        return MGoods::getDetailCtrlOption($model->detail_ctrl); 
                 },
                'filter'=> MGoods::getDetailCtrlOption(),
                'headerOptions' => array('style'=>'width:90px;'),
            ],
			*/
            // 'pics_ctrl',

            /*
            [
                'attribute' => 'pub_userid',
                'label' => '发布者',
                'value'=>function ($model, $key, $index, $column) { 
                        //return MGoods::getDetailCtrlOption($model->detail_ctrl); 
                        $user = MUser::findOne(['id' => $model->pub_userid]);
                        return empty($user)?"":$user->username;
                 },
                'headerOptions' => array('style'=>'width:90px;'),
            ],

            [
                'attribute' => 'status',
                'format' => 'html',
                'label' => '发布状态',
                'value'=>function ($model, $key, $index, $column) {
                     if($model->status == 0)
                            return "<span class=\"label label-danger\">".MGoods::getStatusOption($model->status)."</span>";
                        else
                            return "<span class=\"label label-success\">".MGoods::getStatusOption($model->status)."</span>";
                 },
                'filter'=> MGoods::getStatusOption(),
                'headerOptions' => array('style'=>'width:90px;'),
            ],
            */

            [
                'attribute' => 'status',
                'format' => 'raw',
                'value' => function($model, $key, $index, $column){
                    $editableStr =  Editable::widget([

                        'name'=>'status',
                        'attribute'=>'status',
                        'value' => $model->status,
                        //'asPopover' => true,
                        'header' => '商品发布状态',
                        //'format' => Editable::FORMAT_BUTTON,
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,
                        'data' => [0 => '下架', 1 => '发布'],
                        'options' => ['class'=>'form-control', 'prompt'=>'是否发布商品 ...'],
                        'displayValueConfig'=> [
                            '0' => '<i class="glyphicon glyphicon-remove"></i> 下架',
                            '1' => '<i class="glyphicon glyphicon-ok"></i> 发布',
                        ],

                        'formOptions'=>[
                            'method'=>'post',
                            'action'=> Yii::$app->urlManager->createAbsoluteUrl(['/goods/editable-status','id'=>$model->goods_id])
                        ],
                    ]);
                    return $editableStr;
                },
                'filter'=> MGoods::getStatusOption(),
                'headerOptions' => array('style'=>'width:100px;'),
            ],

            [
                'attribute' => 'pub_type',
                'label' => '发布类型',
                'value'=>function ($model, $key, $index, $column) {
                    return MGoods::getPubTypeOption($model->pub_type);
                },
                'filter'=> MGoods::getPubTypeOption(),
                'headerOptions' => array('style'=>'width:90px;'),
            ],
            [
                'attribute' => 'goods_neworold',
                'label' => '成色',
                'value'=>function ($model, $key, $index, $column) {
                    //若发布类型为租赁，则商品成色为0空 ， 1新品2二手
                    if($model->pub_type == 1)
                        return '--';
                    else
                        return MGoods::getGoodsNeworoldOption($model->goods_neworold);
                },
                'filter'=> MGoods::getGoodsNeworoldOption(),
                'headerOptions' => array('style'=>'width:60px;'),
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'headerOptions' => array('style'=>'width:90px;'),   
            ],
        ],
    ]); ?>

</div>